package com.mall.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 */
@Configuration
public class DruidConfiguration {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource myDruidDataSource(){
        return new DruidDataSource();
    }


    /**
     *  配置druid监控
     *  配置一个管理后台的servlet
     * */
    @Bean
    public ServletRegistrationBean StatViewServlet(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map<String,String> initParams = new HashMap<>();

        initParams.put("loginUsername","zhaoren");
        initParams.put("loginPassword","123456");
        initParams.put("allow" , "");
        initParams.put("Denny" , "");

        servletRegistrationBean.setInitParameters(initParams);
        return servletRegistrationBean;
    }


    /**
     * 设置web监控filter
     * */
    @Bean
    public FilterRegistrationBean WebStatFilter(){
        FilterRegistrationBean filter=new FilterRegistrationBean();

        filter.setFilter(new WebStatFilter());

        Map<String, String> initParams=new HashMap<>();

        initParams.put("exclusions" , "*.css,*.js,/druid/*");

        filter.setInitParameters(initParams);

        filter.setUrlPatterns(Arrays.asList("/*"));

        return filter;
    }


}
